{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 12.13 \u591a\u4e2a\u7ebf\u7a0b\u961f\u5217\u8f6e\u8be2\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u6709\u4e00\u4e2a\u7ebf\u7a0b\u961f\u5217\u96c6\u5408\uff0c\u60f3\u4e3a\u5230\u6765\u7684\u5143\u7d20\u8f6e\u8be2\u5b83\u4eec\uff0c\n\u5c31\u8ddf\u4f60\u4e3a\u4e00\u4e2a\u5ba2\u6237\u7aef\u8bf7\u6c42\u53bb\u8f6e\u8be2\u4e00\u4e2a\u7f51\u7edc\u8fde\u63a5\u96c6\u5408\u7684\u65b9\u5f0f\u4e00\u6837\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u8f6e\u8be2\u95ee\u9898\u7684\u4e00\u4e2a\u5e38\u89c1\u89e3\u51b3\u65b9\u6848\u4e2d\u6709\u4e2a\u5f88\u5c11\u6709\u4eba\u77e5\u9053\u7684\u6280\u5de7\uff0c\u5305\u542b\u4e86\u4e00\u4e2a\u9690\u85cf\u7684\u56de\u8def\u7f51\u7edc\u8fde\u63a5\u3002\n\u672c\u8d28\u4e0a\u8bb2\u5176\u601d\u60f3\u5c31\u662f\uff1a\u5bf9\u4e8e\u6bcf\u4e2a\u4f60\u60f3\u8981\u8f6e\u8be2\u7684\u961f\u5217\uff0c\u4f60\u521b\u5efa\u4e00\u5bf9\u8fde\u63a5\u7684\u5957\u63a5\u5b57\u3002\n\u7136\u540e\u4f60\u5728\u5176\u4e2d\u4e00\u4e2a\u5957\u63a5\u5b57\u4e0a\u9762\u7f16\u5199\u4ee3\u7801\u6765\u6807\u8bc6\u5b58\u5728\u7684\u6570\u636e\uff0c\n\u53e6\u5916\u4e00\u4e2a\u5957\u63a5\u5b57\u88ab\u4f20\u7ed9 select() \u6216\u7c7b\u4f3c\u7684\u4e00\u4e2a\u8f6e\u8be2\u6570\u636e\u5230\u8fbe\u7684\u51fd\u6570\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u6f14\u793a\u4e86\u8fd9\u4e2a\u601d\u60f3\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import queue\nimport socket\nimport os\n\nclass PollableQueue(queue.Queue):\n    def __init__(self):\n        super().__init__()\n        # Create a pair of connected sockets\n        if os.name == 'posix':\n            self._putsocket, self._getsocket = socket.socketpair()\n        else:\n            # Compatibility on non-POSIX systems\n            server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n            server.bind(('127.0.0.1', 0))\n            server.listen(1)\n            self._putsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n            self._putsocket.connect(server.getsockname())\n            self._getsocket, _ = server.accept()\n            server.close()\n\n    def fileno(self):\n        return self._getsocket.fileno()\n\n    def put(self, item):\n        super().put(item)\n        self._putsocket.send(b'x')\n\n    def get(self):\n        self._getsocket.recv(1)\n        return super().get()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u4e2a\u4ee3\u7801\u4e2d\uff0c\u4e00\u4e2a\u65b0\u7684 Queue \u5b9e\u4f8b\u7c7b\u578b\u88ab\u5b9a\u4e49\uff0c\u5e95\u5c42\u662f\u4e00\u4e2a\u88ab\u8fde\u63a5\u5957\u63a5\u5b57\u5bf9\u3002\n\u5728Unix\u673a\u5668\u4e0a\u7684 socketpair() \u51fd\u6570\u80fd\u8f7b\u677e\u7684\u521b\u5efa\u8fd9\u6837\u7684\u5957\u63a5\u5b57\u3002\n\u5728Windows\u4e0a\u9762\uff0c\u4f60\u5fc5\u987b\u4f7f\u7528\u7c7b\u4f3c\u4ee3\u7801\u6765\u6a21\u62df\u5b83\u3002\n\u7136\u540e\u5b9a\u4e49\u666e\u901a\u7684 get() \u548c put() \u65b9\u6cd5\u5728\u8fd9\u4e9b\u5957\u63a5\u5b57\u4e0a\u9762\u6765\u6267\u884cI/O\u64cd\u4f5c\u3002\nput() \u65b9\u6cd5\u518d\u5c06\u6570\u636e\u653e\u5165\u961f\u5217\u540e\u4f1a\u5199\u4e00\u4e2a\u5355\u5b57\u8282\u5230\u67d0\u4e2a\u5957\u63a5\u5b57\u4e2d\u53bb\u3002\n\u800c get() \u65b9\u6cd5\u5728\u4ece\u961f\u5217\u4e2d\u79fb\u9664\u4e00\u4e2a\u5143\u7d20\u65f6\u4f1a\u4ece\u53e6\u5916\u4e00\u4e2a\u5957\u63a5\u5b57\u4e2d\u8bfb\u53d6\u5230\u8fd9\u4e2a\u5355\u5b57\u8282\u6570\u636e\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "fileno() \u65b9\u6cd5\u4f7f\u7528\u4e00\u4e2a\u51fd\u6570\u6bd4\u5982 select() \u6765\u8ba9\u8fd9\u4e2a\u961f\u5217\u53ef\u4ee5\u88ab\u8f6e\u8be2\u3002\n\u5b83\u4ec5\u4ec5\u53ea\u662f\u66b4\u9732\u4e86\u5e95\u5c42\u88ab get() \u51fd\u6570\u4f7f\u7528\u5230\u7684socket\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u800c\u5df2\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4e3a\u5230\u6765\u7684\u5143\u7d20\u76d1\u63a7\u591a\u4e2a\u961f\u5217\u7684\u6d88\u8d39\u8005\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import select\nimport threading\n\ndef consumer(queues):\n    '''\n    Consumer that reads data on multiple queues simultaneously\n    '''\n    while True:\n        can_read, _, _ = select.select(queues,[],[])\n        for r in can_read:\n            item = r.get()\n            print('Got:', item)\n\nq1 = PollableQueue()\nq2 = PollableQueue()\nq3 = PollableQueue()\nt = threading.Thread(target=consumer, args=([q1,q2,q3],))\nt.daemon = True\nt.start()\n\n# Feed data to the queues\nq1.put(1)\nq2.put(10)\nq3.put('hello')\nq2.put(15)\n..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u8bd5\u7740\u8fd0\u884c\u5b83\uff0c\u4f60\u4f1a\u53d1\u73b0\u8fd9\u4e2a\u6d88\u8d39\u8005\u4f1a\u63a5\u53d7\u5230\u6240\u6709\u7684\u88ab\u653e\u5165\u7684\u5143\u7d20\uff0c\u4e0d\u7ba1\u5143\u7d20\u88ab\u653e\u8fdb\u4e86\u54ea\u4e2a\u961f\u5217\u4e2d\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u8f6e\u8be2\u975e\u7c7b\u6587\u4ef6\u5bf9\u8c61\uff0c\u6bd4\u5982\u961f\u5217\u901a\u5e38\u90fd\u662f\u6bd4\u8f83\u68d8\u624b\u7684\u95ee\u9898\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u4e0d\u4f7f\u7528\u4e0a\u9762\u7684\u5957\u63a5\u5b57\u6280\u672f\uff0c\n\u4f60\u552f\u4e00\u7684\u9009\u62e9\u5c31\u662f\u7f16\u5199\u4ee3\u7801\u6765\u5faa\u73af\u904d\u5386\u8fd9\u4e9b\u961f\u5217\u5e76\u4f7f\u7528\u4e00\u4e2a\u5b9a\u65f6\u5668\u3002\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import time\ndef consumer(queues):\n    while True:\n        for q in queues:\n            if not q.empty():\n                item = q.get()\n                print('Got:', item)\n\n        # Sleep briefly to avoid 100% CPU\n        time.sleep(0.01)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u6837\u505a\u5176\u5b9e\u4e0d\u5408\u7406\uff0c\u8fd8\u4f1a\u5f15\u5165\u5176\u4ed6\u7684\u6027\u80fd\u95ee\u9898\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u65b0\u7684\u6570\u636e\u88ab\u52a0\u5165\u5230\u4e00\u4e2a\u961f\u5217\u4e2d\uff0c\u81f3\u5c11\u8981\u82b110\u6beb\u79d2\u624d\u80fd\u88ab\u53d1\u73b0\u3002\n\u5982\u679c\u4f60\u4e4b\u524d\u7684\u8f6e\u8be2\u8fd8\u8981\u53bb\u8f6e\u8be2\u5176\u4ed6\u5bf9\u8c61\uff0c\u6bd4\u5982\u7f51\u7edc\u5957\u63a5\u5b57\u90a3\u8fd8\u4f1a\u6709\u66f4\u591a\u95ee\u9898\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u540c\u65f6\u8f6e\u8be2\u5957\u63a5\u5b57\u548c\u961f\u5217\uff0c\u4f60\u53ef\u80fd\u8981\u50cf\u4e0b\u9762\u8fd9\u6837\u4f7f\u7528\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import select\n\ndef event_loop(sockets, queues):\n    while True:\n        # polling with a timeout\n        can_read, _, _ = select.select(sockets, [], [], 0.01)\n        for r in can_read:\n            handle_read(r)\n        for q in queues:\n            if not q.empty():\n                item = q.get()\n                print('Got:', item)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u65b9\u6848\u901a\u8fc7\u5c06\u961f\u5217\u548c\u5957\u63a5\u5b57\u7b49\u540c\u5bf9\u5f85\u6765\u89e3\u51b3\u4e86\u5927\u90e8\u5206\u7684\u95ee\u9898\u3002\n\u4e00\u4e2a\u5355\u72ec\u7684 select() \u8c03\u7528\u53ef\u88ab\u540c\u65f6\u7528\u6765\u8f6e\u8be2\u3002\n\u4f7f\u7528\u8d85\u65f6\u6216\u5176\u4ed6\u57fa\u4e8e\u65f6\u95f4\u7684\u673a\u5236\u6765\u6267\u884c\u5468\u671f\u6027\u68c0\u67e5\u5e76\u6ca1\u6709\u5fc5\u8981\u3002\n\u751a\u81f3\uff0c\u5982\u679c\u6570\u636e\u88ab\u52a0\u5165\u5230\u4e00\u4e2a\u961f\u5217\uff0c\u6d88\u8d39\u8005\u51e0\u4e4e\u53ef\u4ee5\u5b9e\u65f6\u7684\u88ab\u901a\u77e5\u3002\n\u5c3d\u7ba1\u4f1a\u6709\u4e00\u70b9\u70b9\u5e95\u5c42\u7684I/O\u635f\u8017\uff0c\u4f7f\u7528\u5b83\u901a\u5e38\u4f1a\u83b7\u5f97\u66f4\u597d\u7684\u54cd\u5e94\u65f6\u95f4\u5e76\u7b80\u5316\u7f16\u7a0b\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}